<template>
  <div class="margin_30">
    <el-card shadow="never" :body-style="{ padding: '20px' }">
      <div slot="header">
        <div>
          <el-button type="primary" size="mini" @click="addBrand(0)">新增</el-button>
        </div>
      </div>
      <!-- card body -->
      <el-table :data="brandList" border stripe height="550">
        <el-table-column
          label="序号"
          width="50"
          type="index"
          fixed="left"
        />
        <el-table-column
          prop="name"
          label="品牌名称"
          align="center"
          width="150"
        />
        <el-table-column
          label="展示状态"
          align="center"
          width="150"
        >
          <template v-slot="scope">
            <!-- showStatus (integer, optional): 是否展示: 0-> 否 , 1-> 是 , -->
            <div>
              是否展示：<el-switch v-model="scope.row.showStatus" :active-value="1" :inactive-value="0" @change="isShowStatus(scope.row.id, scope.row.showStatus)" />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="logo"
          label="logo"
          align="center"
          width="200"
        >
          <template v-slot="scope">
            <img style="width:100px;height:100px" :src="scope.row.logo" alt="">
          </template>
        </el-table-column>
        <el-table-column
          prop="bigPic"
          label="专区大图"
          align="center"
          width="200"
        >
          <template v-slot="scope">
            <img style="width:100px;height:100px" :src="scope.row.bigPic" alt="">
          </template>
        </el-table-column>
        <el-table-column
          prop="brandStory"
          label="品牌故事"
          width="180"
          align="center"
        />

        <el-table-column
          prop="createTime"
          label="创建时间"
          width="180"
          align="center"
        />
        <el-table-column
          prop="sort"
          label="排序"
          width="180"
          align="center"
        />
        <el-table-column
          prop="productCommentCount"
          label="产品评论数量"
          width="180"
          align="center"
        />
        <el-table-column
          prop="productCount"
          label="产品数量"
          width="180"
          align="center"
        />
        <!-- factoryStatus (integer, optional): 是否为品牌制造商：0->不是；1->是 , -->
        <el-table-column
          label="是否品牌制造商"
          width="180"
          align="center"
        >
          <template v-slot="scope">
            {{ scope.row.factoryStatus?'是':'不是' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="goEdit(scope.row,1)">编辑</el-button>
            <el-button style="color:red;" size="mini" type="text" @click="delBrand(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="paginationParams.start"
        :page-sizes="paginationParams.pageSize"
        :page-size="paginationParams.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="paginationParams.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 编辑/新增 广告弹框 -->
    <el-dialog title="品牌详情" :visible.sync="isShowDialog" width="65%" size="mini" @close="handleDialog">
      <el-form :model="form" label-width="140px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="品牌名称" style="width:85%">
              <el-input v-model="form.name" placeholder="品牌名称" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首字母" style="width:85%">
              <el-input v-model="form.firstLetter" placeholder="首字母" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" style="width:85%">
              <el-input v-model="form.sort" placeholder="排序" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品数量">
              <el-input v-model="form.productCount" style="width:85%" placeholder="产品数量" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否展示" style="width:85%">
              <el-switch
                v-model="form.showStatus"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否为品牌制造商" style="width:85%">
              <el-switch
                v-model="form.factoryStatus"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="上传专区大图" style="width:85%">
              <el-upload
                class="upload-demo"
                :action="uploadFileOss"
                :headers="token"
                :show-file-list="false"
                :on-success="uploadBigPic"
              >
                <img v-if="form.bigPic" style="width:100px;height:100px" :src="form.bigPic" alt="">
                <i v-else class="el-icon-plus" style="font-size:28px;width:150px;height:150px;padding-top:50px" />
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上传Logo" style="width:85%">
              <el-upload
                class="upload-demo"
                :action="uploadFileOss"
                :headers="token"
                :show-file-list="false"
                :on-success="uploadLogo"
              >
                <img v-if="form.logo" style="width:100px;height:100px" :src="form.logo" alt="">
                <i v-else class="el-icon-plus" style="font-size:28px;width:150px;height:150px;padding-top:50px" />
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleDialog">取 消</el-button>
        <el-button type="primary" @click="saveBrand">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { findBrandByPage, addBrand, updateBrand, delBrand } from '@/api/product/brand'
import mix from '@/mixins'
export default {
  mixins: [mix],

  data() {
    return {
      isShowDialog: false,
      brandList: [],
      form: {
        'bigPic': '',
        'brandStory': '',
        'createTime': '',
        'factoryStatus': 0,
        'firstLetter': '',
        'id': '',
        'logo': '',
        'modifyTime': '',
        'name': '',
        'productCommentCount': '',
        'productCount': '',
        'showStatus': 0,
        'sort': ''
      },
      state: 0
    }
  },

  created() {
    this.getBrand()
  },

  mounted() {

  },

  methods: {
    // 上传图片
    uploadLogo(e) {
      this.form.logo = e.data.fileUrl
    },
    uploadBigPic(e) {
      this.form.bigPic = e.data.fileUrl
    },
    goEdit(val, state) {
      this.state = state
      this.isShowDialog = true
      this.form = Object.assign({ }, val)
    },
    // 删除品牌
    delBrand(id) {
      delBrand(id)
        .then(res => {
          this.$confirm('此操作将永久删除该品牌, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          }).then(() => {
            this.isShowDialog = false
            this.getBrand()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        })
    },
    // 获取品牌数据
    getBrand() {
      findBrandByPage(this.paginationParams.start, this.paginationParams.limit)
        .then(res => {
          // console.log(res)
          // res.data.rows.forEach(ele => {
          //   ele.factoryStatus = ele.factoryStatus ? '是' : '不是'
          // })
          this.brandList = res.data.rows
          this.paginationParams.total = res.data.total
        })
    },
    // 每页多少条改变时触发（main.js中切换页码中文）
    handleSizeChange(e) {
      this.paginationParams.limit = e
      this.getBrand()
    },
    // 当前页码发生改变触发
    handleCurrentChange(e) {
      this.paginationParams.start = e
      this.getBrand()
    },
    // 新增按钮 弹框出现
    addBrand(state) {
      this.state = state
      this.isShowDialog = true
    },
    // 取消新增
    handleDialog() {
      this.isShowDialog = false
      this.form = {}
    },
    // 确定新增品牌
    saveBrand() {
      this.form.factoryStatus = this.form.factoryStatus ? 1 : 0
      this.form.showStatus = this.form.showStatus ? 1 : 0
      // console.log(this.form)
      if (this.state) {
        // 修改广告
        updateBrand(this.form)
          .then(res => {
            // 成功后关闭弹窗
            this.isShowDialog = false
            // 重新获取品牌列表
            this.getBrand()
          })
      } else {
        // 新增广告
        addBrand(this.form)
          .then(res => {
            this.isShowDialog = false
            this.getBrand()
          })
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
